package com.itisyue.fun.controller;

import com.alibaba.fastjson.JSONObject;
import com.itisyue.fun.advice.RestResult;
import com.itisyue.fun.advice.ResultUtil;
import com.itisyue.fun.utils.EncryptUtil;
import com.itisyue.fun.utils.OfficeUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;

@RestController
public class TestController {
    private Logger log = LoggerFactory.getLogger(TestController.class);

    @Value("${fileStore.urlPrefix:}")
    private String fileStoreUrlPrefix;

    @Qualifier("taskExecutor")
    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;

    @GetMapping("/thread/{time}")
    public RestResult<String> threadTime(@PathVariable("time") Long time, HttpServletResponse resp) throws Exception {

        if(time<0){
            time = 1000L;
        }
        Thread.sleep(time);

        return ResultUtil.success("ok");
    }

    @GetMapping("/fileStore/{fileName}")
    public RestResult<String> fileStore(@PathVariable("fileName") String fileName, HttpServletResponse resp) throws Exception {

        String pub = "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEWuTGiihcA0P09ansdBX0X7R2OOACTEsRYwpUc97r8yq4iIHvM2MjklumD8wJQvdDMjsSMUdwmWGjHm8r11bZgQ==";
        String url = EncryptUtil.fileDownloadEncrypt("/home/nasdisk/ecl/temp/fileStore/"+fileName, pub);

        String finaleUrl = fileStoreUrlPrefix+url;
        return ResultUtil.success(finaleUrl);
    }
    @GetMapping("/redirect/{id}")
    public void redirect(@PathVariable("id") String id, HttpServletResponse resp) throws IOException {
        String redirectUri = "http://127.0.0.1/jiaju/index2.html";
        resp.sendRedirect(redirectUri);
    }

    /**
     * 下载接口
     */
    @RequestMapping(value = "/download")
    @ResponseBody
    public ResponseEntity<byte[]> download()throws Exception{
        String fileUrl = "http://192.168.37.163/img/20191113/f222ce6d6f0879bcf91ffc7c85b2c5bc.jpg";
        String fileName = "孙艺珍.jpg";
        HttpHeaders headers = new HttpHeaders();
        //指定文件名
        //headers.setContentDispositionFormData("attachment", new String(fileName.getBytes("UTF-8"),"ISO-8859-1"));
        //指定以流的形式下载文件
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        //图片url转二进制数组
        byte[] bytes = OfficeUtil.getInputStreamByUrl("C:\\Users\\itisyue\\Desktop\\test\\01pr.mp4", true);
        return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
    }


    @GetMapping("/getTest/{id}")
    @ResponseBody
    public RestResult<Integer> getTest(@PathVariable Integer id) throws Exception{
        if(id==null || id <1){
            throw new IllegalArgumentException("id < 1");
        }
        return ResultUtil.success(id);
    }

    @PostMapping("/uploadTest")
    @ResponseBody
    public JSONObject upload(HttpServletRequest request){//@RequestParam("file") MultipartFile file, String id,
        System.out.println("upload");
        JSONObject jsonObject = new JSONObject();
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        if(CollectionUtils.isEmpty(files)){
            jsonObject.put("success", false);
            jsonObject.put("msg", "文件为空");
            System.out.println("文件为空");
            return jsonObject;
        }
        MultipartFile file = files.get(0);

        jsonObject.put("success", true);
        jsonObject.put("size", file.getSize());
        System.out.println("计算大小完毕");
        return jsonObject;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()){
            String s = sc.nextLine();
            System.out.println(s);
        }
    }
}
